package com.zyb.beidada.scoring;

import com.zyb.beidada.common.ErrorCode;
import com.zyb.beidada.exception.BusinessException;
import com.zyb.beidada.model.entity.App;
import com.zyb.beidada.model.entity.UserAnswer;
import org.checkerframework.checker.units.qual.C;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 十八岁讨厌编程
 * @date 2025/5/6 21:38
 * @PROJECT_NAME beidada-backend
 * @description 声明式式策略执行器
 */

@Component
public class ScoringStrategyExecutor {

    @Resource
    private List<ScoringStrategy> scoringStrategyList;

    public UserAnswer doScore(List<String> choices, App app) throws Exception {
        //获取app的类型以及评分策略
        Integer scoringStrategy = app.getScoringStrategy();
        Integer appType = app.getAppType();
        if (appType == null || scoringStrategy == null) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "应用配置有误，未找到匹配的策略");
        }
        //通过策略上的注解，决定使用哪一个注解
        for (ScoringStrategy strategy : scoringStrategyList) {
            ScoringStrategyRequire annotation = strategy.getClass().getAnnotation(ScoringStrategyRequire.class);
            if (appType.equals(annotation.appType()) && scoringStrategy.equals(annotation.scoringStrategy())) {
                return strategy.doScore(choices,app);
            }
        }
        throw new BusinessException(ErrorCode.SYSTEM_ERROR, "应用配置有误，未找到匹配的策略");
    }
}
